﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArraySegment
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr1 = {1, 4, 2, 3, 3, 4};
            int[] arr2 = {3, 4, 2, 3, 42,3,2};
            var segments=new ArraySegment<int>[2]
            {
                new ArraySegment<int>(arr1,0,3),
                new ArraySegment<int>(arr2,3,3)
            };
            Console.WriteLine(SumOfSegment(segments));
            OfficialSample();


            Console.ReadLine();
        }

        public static int SumOfSegment(ArraySegment<int>[] segments)
        {
            int sum = 0;
            foreach (var segment in segments)
            {
                for (int i = segment.Offset; i < segment.Offset + segment.Count; i++)
                {
                    sum += segment.Array[i];
                }
            }

            return sum;
        }

        private static void OfficialSample()
        {
            string[] names = { "Adam", "Bruce", "Charles", "Daniel",
                "Ebenezer", "Francis", "Gilbert",
                "Henry", "Irving", "John", "Karl",
                "Lucian", "Michael" };
            var partNames = new ArraySegment<string>(names, 2, 5);
            // Cast the ArraySegment object to an IList<String> and enumerate it.
            var list = (IList<String>)partNames;
            for (int ctr = 0; ctr <= partNames.Offset+ partNames.Count; ctr++)
                Console.WriteLine(partNames.Array[ctr]);

            //foreach (var _list in list)
            //{
            //    if (_list == "Bruce")
            //    {
            //        _list = "haha";
            //        continue;
            //    }
            //}
            foreach (var _list in list)
            {
                Console.WriteLine(_list);
            }
        }
    }
}
